﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmThemDi : Form
    {
        CongVanDiDTO _congVanDi = null;

        public CongVanDiDTO CongVanDi
        {
            get { return _congVanDi; }
            set { _congVanDi = value; }
        }
        bool _readOnly = false;
        public frmThemDi()
        {
            InitializeComponent();
        }

        public bool ReadOnly
        {
            get { return _readOnly; }
            set { _readOnly = value; }
        }
        void cmbHeThong_Load()
        {
            cmbHeThong.DisplayMember = "Ten";
            cmbHeThong.ValueMember = "ID";
            cmbHeThong.DataSource = HeThongBUS.LoadAll();
            cmbHeThong.SelectedIndex = -1;
        }
        void cmbLoaiDi_Load()
        {
            cmbLoaiDi.DisplayMember = "Ten";
            cmbLoaiDi.ValueMember = "ID";
            cmbLoaiDi.DataSource = LoaiDiBUS.LoadAll();
            cmbLoaiDi.SelectedIndex = -1;
        }
        void cmbNguoi_Load()
        {
            cmbNguoiKy.DisplayMember = "Ten";
            cmbNguoiKy.ValueMember = "ID";

            List<NguoiDTO> tmp = NguoiBUS.LoadAll();
            tmp.ForEach(GolbalSettings.MarkName);
            cmbNguoiKy.DataSource = tmp;
            cmbNguoiKy.SelectedIndex = -1;
        }

        void lstDanhSachNoiNhan_Load()
        {
            if (cmbHeThong.SelectedIndex == -1)
            {
                return;
            }
            List<NoiNhanDTO> tmp = new List<NoiNhanDTO>();
            if (rdbTen.Checked)
            {
                tmp = NoiNhanBUS.LoadAll(((int)(cmbHeThong.SelectedValue)), false);
            }
            else if (rdbSoLan.Checked)
            {
                tmp = NoiNhanBUS.LoadAll(((int)(cmbHeThong.SelectedValue)), true);
            }

            foreach (NoiNhanDTO item in _congVanDi.NoiNhan)
            {
                for (int i = 0; i < tmp.Count; i++)
                {
                    if (item.HeThong.ID == tmp[i].HeThong.ID && item.ID == tmp[i].ID)
                    {
                        tmp.RemoveAt(i);
                    }
                }
            }

            lstDanhSachNoiNhan.DisplayMember = "Ten";
            lstDanhSachNoiNhan.ValueMember = "ID";
            lstDanhSachNoiNhan.DataSource = tmp;
        }




        void Check()
        {
            if (_readOnly)
            {
                return;
            }
            bool result = true;
            if (cmbLoaiDi.SelectedIndex == -1 || txtSoKyHieu.Text.Trim() == "" || txtSTT.Text.Trim() == "")
            {
                result = false;
            }

            int tmp=0;
            if (!int.TryParse(txtSTT.Text.Trim(), out tmp))
            {
                result = false;
            }
            if (tmp <= 0)
            {
                result = false;
            }


            if (result)
            {
                _congVanDi.Loai = ((LoaiDiDTO)cmbLoaiDi.SelectedItem);
                _congVanDi.Stt = tmp;
                _congVanDi.SoKyHieuVanBan = txtSoKyHieu.Text.Trim();
            }

            if (_congVanDi.NoiNhan.Count == 0)
            {
                result = false;
            }

            if (!int.TryParse(txtSoLuong.Text.Trim(), out tmp))
            {
                result = false;
            }
            if (tmp <= 0)
            {
                result = false;
            }

            if (result)
            {
                _congVanDi.SoLuong = tmp;
            }




            if (result)
            {
                btnThem.Enabled = true;
            }
            else
            {
                btnThem.Enabled = false;
            }
        }

        TextBox CloneTextBox(TextBox original)
        {
            TextBox clone = new TextBox();
            clone.Size = original.Size;
            clone.Font = original.Font;
            clone.BackColor = original.BackColor;
            clone.ReadOnly = original.ReadOnly;
            return clone;
        }




        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void frmThemDi_Load(object sender, EventArgs e)
        {
            if (_congVanDi == null)
            {
                _congVanDi = new CongVanDiDTO();
                _congVanDi.SoLuong = 1;
                txtSoLuong.Text = "1";

                dtpNgayLuu.Value = DateTime.Today;

                cmbHeThong_Load();
                cmbLoaiDi_Load();
                cmbNguoi_Load();

                rdbTen.Checked = true;

                lstNoiNhan.DisplayMember = "Ten";
                lstNoiNhan.ValueMember = "ID";
                lstNoiNhan.DataSource = _congVanDi.NoiNhan;

                dtpNgayKy.Value = DateTime.Today;
                dtpNgayKy.CustomFormat = "  ";
                dtpNgayKy.Enabled = false;



                this.Text = "Them cong van di";
                btnThem.Enabled = false;
            }
            else
            {
                if (!_readOnly)
                {
                    _readOnly = true;
                    dtpNgayLuu.Value = _congVanDi.NgayLuu;


                    cmbHeThong_Load();
                    cmbLoaiDi_Load();
                    cmbNguoi_Load();

                    rdbTen.Checked = true;

                    cmbLoaiDi.SelectedIndex = -1;
                    if (_congVanDi.Loai != null)
                    {
                        for (int i = 0; i < cmbLoaiDi.Items.Count; i++)
                        {
                            if (((LoaiDiDTO)cmbLoaiDi.Items[i]).ID == _congVanDi.Loai.ID)
                            {
                                cmbLoaiDi.SelectedIndex = i;
                            }
                        }
                    }
                    cmbNguoiKy.SelectedIndex = -1;
                    if (_congVanDi.NguoiKy != null)
                    {
                        for (int i = 0; i < cmbNguoiKy.Items.Count; i++)
                        {
                            if (((NguoiDTO)cmbNguoiKy.Items[i]).ID == _congVanDi.NguoiKy.ID)
                            {
                                cmbNguoiKy.SelectedIndex = i;
                            }
                        }
                    }

                    txtSTT.Text = _congVanDi.Stt.ToString();
                    txtSoKyHieu.Text = _congVanDi.SoKyHieuVanBan;
                    txtNoiDung.Text = _congVanDi.NoiDung;
                    txtGhiChu.Text = _congVanDi.GhiChu;
                    txtSoLuong.Text = _congVanDi.SoLuong.ToString();


                    lstNoiNhan.DisplayMember = "Ten";
                    lstNoiNhan.ValueMember = "ID";
                    lstNoiNhan.DataSource = _congVanDi.NoiNhan;

                    if (_congVanDi.NgayKy.HasValue)
                    {
                        chkNgayKy.Checked = true;
                        dtpNgayKy.Value = _congVanDi.NgayKy.Value;
                        dtpNgayKy.CustomFormat = "d/M/yyyy";
                        dtpNgayKy.Enabled = true;
                        btnUp.Enabled = true;
                    }
                    else
                    {
                        chkNgayKy.Checked = false;
                        dtpNgayKy.Value = DateTime.Today;
                        dtpNgayKy.CustomFormat = "  ";
                        dtpNgayKy.Enabled = false;
                        btnUp.Enabled = false;
                    }

                    this.Text = "Sua cong van di";
                    btnThem.Enabled = false;
                    btnThem.Text = "Lưu";
                    btnThem.Click -= btnThem_Click;
                    btnThem.Click += btnLuu_Click;

                    _readOnly = false;
                    Check();
                }
                else
                {
                    txtSTT.ReadOnly = true;
                    txtSoKyHieu.ReadOnly = true;
                    txtNoiDung.ReadOnly = true;
                    txtGhiChu.ReadOnly = true;
                    txtSoLuong.ReadOnly = true;

                    TextBox txtNgayLuu = CloneTextBox(txtSTT);
                    txtNgayLuu.Location = dtpNgayLuu.Location;
                    txtNgayLuu.Text = _congVanDi.NgayLuu.ToString(dtpNgayLuu.CustomFormat);
                    grbSoKyHieu.Controls.Add(txtNgayLuu);

                    TextBox txtLoai = CloneTextBox(txtSoKyHieu);
                    txtLoai.Location = cmbLoaiDi.Location;
                    txtLoai.Text = _congVanDi.Loai.Ten;
                    grbSoKyHieu.Controls.Add(txtLoai);

                    TextBox txtNguoiKy = CloneTextBox(txtSTT);
                    txtNguoiKy.Location = cmbNguoiKy.Location;
                    if (_congVanDi.NguoiKy != null)
                    {
                        txtNguoiKy.Text = _congVanDi.NguoiKy.Ten;
                    }
                    grpKy.Controls.Add(txtNguoiKy);

                    TextBox txtNgayKy = CloneTextBox(txtSoKyHieu);
                    txtNgayKy.Location = dtpNgayKy.Location;
                    if (_congVanDi.NgayKy.HasValue)
                    {
                        txtNgayKy.Text = _congVanDi.NgayKy.Value.ToString(dtpNgayKy.CustomFormat);
                        chkNgayKy.Checked = true;
                    }
                    grpKy.Controls.Add(txtNgayKy);







                    txtSTT.Text = _congVanDi.Stt.ToString();
                    txtSoKyHieu.Text = _congVanDi.SoKyHieuVanBan;
                    txtNoiDung.Text = _congVanDi.NoiDung;
                    txtGhiChu.Text = _congVanDi.GhiChu;
                    txtSoLuong.Text = _congVanDi.SoLuong.ToString();

                    foreach (Control control in grbSoKyHieu.Controls)
                    {
                        Type tmp = control.GetType();
                        if (tmp.Name != "TextBox" && tmp.Name != "Label")
                        {
                            control.Enabled = false;
                            control.Hide();
                        }
                    }
                    foreach (Control control in grbNoiNhan.Controls)
                    {
                        control.Enabled = false;
                        control.Hide();
                    }
                    chkNgayKy.AutoCheck = false;
                    foreach (Control control in grpKy.Controls)
                    {
                        Type tmp = control.GetType();
                        if (tmp.Name != "TextBox" && tmp.Name != "Label" && tmp.Name != "CheckBox")
                        {
                            control.Enabled = false;
                            control.Hide();
                        }
                    }
                    TextBox txtNoiNhan = CloneTextBox(txtSTT);
                    txtNoiNhan.Multiline = true;
                    txtNoiNhan.ScrollBars = ScrollBars.Vertical;
                    grbNoiNhan.Controls.Add(txtNoiNhan);
                    txtNoiNhan.Dock = DockStyle.Fill;
                    foreach (NoiNhanDTO tmp in _congVanDi.NoiNhan)
                    {
                        txtNoiNhan.Text += (tmp.Ten + Environment.NewLine);
                    }


                    this.Text = "Xem chi tiet cong van di";
                    btnThem.Hide();
                    btnThem.Click -= btnThem_Click;

                    /////////////////////////
                }
            }
        }









        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (CongVanDiBUS.Add(_congVanDi))
                {
                    cmbLoaiDi_Load();
                    cmbNguoi_Load();
                    lstDanhSachNoiNhan_Load();
                    chkNgayKy.Checked = false;
                    txtNoiDung.Text = "";
                    txtGhiChu.Text = "";

                    if (!CongVanDiBUS.AddRelationship(_congVanDi))
                    {
                        MessageBox.Show("Không thể thêm một số nơi nhận.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }

                }
                else
                {
                    MessageBox.Show("Không thể thêm công văn mới.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (CongVanDiBUS.Edit(_congVanDi))
                {

                    if (!(CongVanDiBUS.DeleteRelationship(_congVanDi.ID) && CongVanDiBUS.AddRelationship(_congVanDi)))
                    {
                        MessageBox.Show("Không thể cập nhật một số nơi nhan.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                    else
                    {
                        MessageBox.Show("Đã cập nhật dữ liệu.", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    }


                }
                else
                {
                    MessageBox.Show("Không thể cập nhật dữ liệu.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void chkNgayKy_CheckedChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            if (chkNgayKy.Checked)
            {
                dtpNgayKy.CustomFormat = @"d/M/yyyy";
                dtpNgayKy.Enabled = true;
                _congVanDi.NgayKy = dtpNgayKy.Value;
                btnUp.Enabled = true;
            }
            else
            {
                dtpNgayKy.CustomFormat = "  ";
                dtpNgayKy.Enabled = false;
                _congVanDi.NgayKy = null;
                btnUp.Enabled = false;
            }
        }

        private void dtpNgayKy_ValueChanged(object sender, EventArgs e)
        {
            if (_readOnly || !chkNgayKy.Checked)
            {
                return;
            }
            _congVanDi.NgayKy = dtpNgayKy.Value;
        }
        
        private void dtpNgayLuu_ValueChanged(object sender, EventArgs e)
        {
            _congVanDi.NgayLuu = dtpNgayLuu.Value;
        }

        private void btnThemNguoiKy_Click(object sender, EventArgs e)
        {
            frmCanBo frm = new frmCanBo();
            frm.ShowDialog();
            cmbNguoi_Load();
        }

        private void cmbNguoiKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            _congVanDi.NguoiKy = ((NguoiDTO)cmbNguoiKy.SelectedItem);
        }

        private void txtNoiDung_TextChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            _congVanDi.NoiDung = txtNoiDung.Text.Trim();
        }

        private void txtGhiChu_TextChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            _congVanDi.GhiChu = txtGhiChu.Text.Trim();
        }

        private void btnThemLoaiDi_Click(object sender, EventArgs e)
        {
            frmLoaiDi frm = new frmLoaiDi();
            frm.ShowDialog();
            cmbLoaiDi_Load();
        }

        private void cmbLoaiDi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            if (cmbLoaiDi.SelectedIndex == -1)
            {
                txtSTT.Text = "";
                txtSoKyHieu.Text = "";
            }
            else
            {
                txtSTT.Text = (1 + CongVanDiBUS.Count((int)(cmbLoaiDi.SelectedValue))).ToString();
                txtSoKyHieu.Text = txtSTT.Text + "_" + GolbalSettings.Abbr(((LoaiDiDTO)(cmbLoaiDi.SelectedItem)).Ten);
            }
            Check();
        }

        private void txtSTT_TextChanged(object sender, EventArgs e)
        {
            if (cmbLoaiDi.SelectedIndex == -1)
            {
                Check();
                return;
            }
            txtSoKyHieu.Text = txtSTT.Text + "_" + GolbalSettings.Abbr(((LoaiDiDTO)(cmbLoaiDi.SelectedItem)).Ten);
            Check();
        }

        private void txtSoKyHieu_TextChanged(object sender, EventArgs e)
        {
            Check();
        }

        private void rdbTen_CheckedChanged(object sender, EventArgs e)
        {
            lstDanhSachNoiNhan_Load();
        }

        private void rdbSoLan_CheckedChanged(object sender, EventArgs e)
        {
            lstDanhSachNoiNhan_Load();
        }

        private void cmbHeThong_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstDanhSachNoiNhan_Load();
        }

        private void btnThemNoiNhan_Click(object sender, EventArgs e)
        {
            frmNoiNhan frm = new frmNoiNhan();
            frm.ShowDialog();
            _congVanDi.NoiNhan.Clear();
            lstDanhSachNoiNhan_Load();
            ((CurrencyManager)lstNoiNhan.BindingContext[lstNoiNhan.DataSource]).Refresh();
            Check();
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            if (lstNoiNhan.SelectedIndex == -1)
            {
                return;
            }
            _congVanDi.NoiNhan.Remove((NoiNhanDTO)(lstNoiNhan.SelectedItem));
            ((CurrencyManager)lstNoiNhan.BindingContext[lstNoiNhan.DataSource]).Refresh();
            lstDanhSachNoiNhan_Load();
            Check();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            if (lstDanhSachNoiNhan.SelectedIndex == -1)
            {
                return;
            }
            _congVanDi.NoiNhan.Add((NoiNhanDTO)(lstDanhSachNoiNhan.SelectedItem));
            ((CurrencyManager)lstNoiNhan.BindingContext[lstNoiNhan.DataSource]).Refresh();
            lstDanhSachNoiNhan_Load();
            Check();
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            _congVanDi.NoiNhan.Clear();
            ((CurrencyManager)lstNoiNhan.BindingContext[lstNoiNhan.DataSource]).Refresh();
            lstDanhSachNoiNhan_Load();
            Check();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            dtpNgayKy.Value = dtpNgayLuu.Value;
            chkNgayKy.Checked = true;
        }

        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {
            Check();
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            dtpNgayLuu.Value = dtpNgayKy.Value;
        }
    }
}
